// pages/orderlist/backoff/backoffdetail.js
var goodsStatusDesc = ["已收到货", "未收到货"];
var reasons = ["拍错/不喜欢/效果不好", "版本/批次/颜色/容量等与商品描述不符", "卖家发错货", "假冒品牌", "收到商品少件/破损/污渍等", "其他"];
var notesStr = '';
var pic_urls;
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var backofftype = options.backofftype;
    if(backofftype == '1'||backofftype =='2'){
      wx.setNavigationBarTitle({
        title: '申请退款',
      });
    }
    else{
      wx.setNavigationBarTitle({
        title: '申请换货',
      });
    }
    var goodsinfo = JSON.parse(options.goods);
    console.log(goodsinfo);
    pic_urls = [];
    this.setData({
      goods_status: "请选择",
      goods_reason: "请选择",
      pics: [],
      backofftype: backofftype,
      goodsinfo: goodsinfo
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.shareToIndex();
  },
  chooseImage: function () {
    var that = this;
    wx.chooseImage({
      count: 6,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var pics = [];
        if (that.data['pics']) {
          pics = that.data['pics'];
        }
        if (pics.length > 6) {
          return;
        }
        var tempFilePaths = res.tempFilePaths;
        for (var i = 0; i < tempFilePaths.length; i++) {
          if (pics.length >= 6) {
            break;
          }
          var picinfo = {};
          picinfo.note = "添加说明";
          picinfo.url = tempFilePaths[i];
          pics.push(picinfo);
        }
        that.setData({
          pics: pics
        });
      }
    })
  },
  picDelete: function (event) {
    var picindex = parseInt(event.currentTarget.dataset.picindex);
    var pics = this.data['pics'];
    pics.splice(picindex, 1);
    this.setData({
      pics: pics
    });
  },
  goodsStatus: function () {
    var that = this;
    wx.showActionSheet({
      itemList: goodsStatusDesc,
      success: function (res) {
        that.setData({
          goods_status: goodsStatusDesc[res.tapIndex]
        });
      }
    })
  },
  backoffReason: function () {
    var backofftype = this.data['backofftype'];
    var status = this.data['goods_status'];
    if (backofftype == "1" && status == "请选择") {
      return;
    }
    var that = this;
    wx.showActionSheet({
      itemList: reasons,
      success: function (res) {
        that.setData({
          goods_reason: reasons[res.tapIndex]
        });
      }
    });
  },
  notesInput: function (event) {
    notesStr = event.detail.value;
  },
  submitReturnOrder: function () {
    if (this.data.goods_reason == '请选择' || this.data.goods_reason == '') {
      wx.showToast({
        title: '请选择理由',
      });
      return;
    }
    app.showLoading();
    this.uploadPic(0);
  },
  uploadPic: function (pindex) {
    var pics = this.data.pics;
    if (pindex >= pics.length) {
      this.submitInfo();
      return;
    }
    var that = this;
    var url = app.globalData.webUrl + '/index.php/api/Order/return_goods_upload/';
    wx.uploadFile({
      url: url,
      filePath: pics[pindex].url,
      name: 'file',
      success: function (res) {
        res = JSON.parse(res.data);
        if (res.status == 'ok') {
          pic_urls.push(res.result);
        }
        that.uploadPic(pindex + 1);
      },
      fail: function (res) {
        // console.log(res);
      }
    })
  },
  submitInfo: function () {
    var backtype = parseInt(this.data.backofftype) - 1;
    var isReceive = 0;
    if (backtype == 1 || backtype == 2) {
      isReceive = 1;
    }
    else if (backtype == 0) {
      if (this.data.goods_status == '已收到货') {
        isReceive = 1;
      }
    }
    var imgs = pic_urls.join(',');
    // console.log(imgs);
    var info = {
      type: backtype,
      goods_id: this.data.goodsinfo.goodsid,
      goods_num: this.data.goodsinfo.buycount,
      is_receive: isReceive,
      describe: notesStr,
      order_id: this.data.goodsinfo.orderid,
      order_sn: this.data.goodsinfo.ordersn,
      reason: this.data.goods_reason,
      rec_id: this.data.goodsinfo.recid,
      spec_key: this.data.goodsinfo.speckey,
      imgs: imgs
    };
    var url = '/index.php/api/Order/return_goods';
    app.request(url,info).then(this.returnGoodsCallBack.bind(this));
  },
  returnGoodsCallBack: function (res) {
    app.hideLoading();
    wx.showToast({
      icon: 'none',
      title: res.msg,
      duration: 2000,
    });
    var delayid = setTimeout(function(){
      clearTimeout(delayid);
      wx.navigateBack({});
    },2000);
  }
})